package com.zivwu.library.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.zivwu.library.utils.ToastUtil;

/**
 * App中Activity的基类
 */

public abstract class IActivity extends RxAppCompatActivity {
    /**
     * ROOTVIEW上的加载进度条
     */
    private ProgressBar progressBar;


    /**
     * 启动activity
     * @param cls activity class
     */
    public void startActivity(Class<?> cls) {
        Bundle bundle = new Bundle();
        startActivity(cls, bundle);
    }

    /**
     * 启动activity
     *
     * @param cls    activity class
     * @param bundle 存入intent 的bundle对象
     */
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);

    }


    /**
     * 为FrameLayout添加一个ProgressBar
     *  @param activity                    所需要添加的activity
     * @param customIndeterminateDrawable ProgressBar图形
     */
    protected ProgressBar createProgressBar(Activity activity, @Nullable Drawable customIndeterminateDrawable) {
        // activity根部的ViewGroup，其实是一个FrameLayout
        FrameLayout rootContainer = (FrameLayout) activity.findViewById(android.R.id.content);
        // 给progressbar准备一个FrameLayout的LayoutParams
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置对其方式为：屏幕居中对其
        lp.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;

        ProgressBar progressBar = new ProgressBar(activity);
        progressBar.setVisibility(View.GONE);
        progressBar.setLayoutParams(lp);
        // 自定义小菊花
        if (customIndeterminateDrawable != null) {
            progressBar.setIndeterminateDrawable(customIndeterminateDrawable);
        }
        // 将菊花添加到FrameLayout中
        rootContainer.addView(progressBar);
        this.progressBar = progressBar;
        return progressBar;
    }




    /**
     * 进行一个toast提示
     *
     * @param msg 需要显示的MESSAGE
     */
    public void showToast(String msg) {
        ToastUtil.showToast(this.getApplicationContext(), msg);
    }

    public void showToast(@StringRes int  id) {
       showToast(this.getString(id));
    }






}
